package com.wr.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.wr.mapper.ArticleMapper;
import com.wr.model.Article;
import com.wr.service.ArticleService;
import com.wr.utils.PageData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author weirong
 * @Title:
 * @Description:
 * @date 2018/6/1117:58
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    RedisTemplate redisTemplate;

    private String BLOG_DETAIL_CACHE_KEY = "blog:detail:cache:";

    public int deleteByPrimaryKey(Integer id) {
        return articleMapper.deleteByPrimaryKey(id);
    }

    public int insertSelective(Article record) {
        return articleMapper.insertSelective(record);
    }

    public Article selectByPrimaryKey(Integer id) {
        String key = BLOG_DETAIL_CACHE_KEY+id;
        Object o = redisTemplate.opsForValue().get(key);
        String artJson = null;
        if(o != null) {
            artJson = String.valueOf(o);
            return JSON.parseObject(artJson,Article.class);
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article != null) {
            artJson = JSON.toJSONString(article);
            redisTemplate.opsForValue().set(key,artJson,30, TimeUnit.SECONDS);
        }
        return article;
    }

    public int updateByPrimaryKeySelective(Article record) {
        return articleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Article> listPage(Article record,Integer pageNo) {

        String key = "blog:main:list:"+pageNo;
        Object o = redisTemplate.opsForValue().get(key);
        String artJson = null;
        if(o != null) {
            artJson = String.valueOf(o);
            return JSON.parseArray(artJson,Article.class);
        }
        PageHelper.startPage(pageNo,5);
        List<Article> articles = articleMapper.listPage(record);
        artJson = JSON.toJSONString(articles);
        redisTemplate.opsForValue().set(key,artJson,30, TimeUnit.SECONDS);

        return articles;
    }

    @Override
    public void updateViewCount(Integer id) {
        articleMapper.updateViewCount(id);
    }

    @Override
    public List<Article> listPd(PageData pd) {
        StringBuffer s = new StringBuffer();

        String key = "blog:main:right:"+pd.getString("order");
        Object o = redisTemplate.opsForValue().get(key);
        String artJson = null;
        if(o != null) {
            artJson = String.valueOf(o);
            return JSON.parseArray(artJson,Article.class);
        }
        List<Article> articles = articleMapper.listPd(pd);
        artJson = JSON.toJSONString(articles);
        redisTemplate.opsForValue().set(key,artJson,30, TimeUnit.SECONDS);
        return articles;
    }
}
